package edu.suda.LoopCodePlus.controller;

import edu.suda.LoopCodePlus.common.Result;
import edu.suda.LoopCodePlus.entity.Store;
import edu.suda.LoopCodePlus.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/store")
@Slf4j
public class StoreController {
    @Autowired
    private StoreService storeService;
    @GetMapping("/list")
    public Result list(@RequestParam("page") Integer page) {
        System.out.println("开始加载商店");
        List<Store> storeList;
        try {
            System.out.println("加载商店中");
            storeList = storeService.findAll();
        }catch (Exception e){
            System.out.println("加载商店失败");
            return Result.failure("加载商店失败");
        }
        System.out.println("加载商店成功,商品如下:");
        System.out.println(storeList.subList((page-1)*6, Math.min(storeList.size(), page * 6)));
        return Result.success(storeList.subList((page-1)*6, Math.min(storeList.size(), page * 6)), storeList.size());
    }
}
